package cn.smbms.service.user;

import cn.smbms.dao.user.UserDao;
import cn.smbms.pojo.User;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Connection;
import java.util.List;

/**
 * service层捕获异常，进行事务处理
 * 事务处理：调用不同dao的多个方法，必须使用同一个connection（connection作为参数传递）
 * 事务完成之后，需要在service层进行connection的关闭，在dao层关闭（PreparedStatement和ResultSet对象）
 * @author Administrator
 *
 */
@Service("user")
public class UserServiceImpl implements UserService {
	@Resource
	private UserDao userDao;
//	public UserServiceImpl(){
//		userDao = new UserDaoImpl();
//	}

	SqlSessionFactory sqlSessionFactory=null;
	@Override
	public boolean add(User user) {
		// TODO Auto-generated method stub
		
		boolean flag = false;
		Connection connection = null;
		SqlSession session=null;
		try {
//			connection = BaseDao.getConnection();
//			connection.setAutoCommit(false);//开启JDBC事务管理
//			int updateRows = userDao.add(connection,user);
//			connection.commit();

//             sqlSessionFactory=utils.open();
//			 session=sqlSessionFactory.openSession(true);
//			int updateRows=session.getMapper(UserDao.class).add(user);
			int updateRows=userDao.add(user);
			if(updateRows > 0){
				flag = true;
				System.out.println("add success!");
			}else{
				System.out.println("add failed!");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
//			try {
//				System.out.println("rollback==================");
//				connection.rollback();
//			} catch (SQLException e1) {
//				// TODO Auto-generated catch block
//				e1.printStackTrace();
//			}
		}finally{
			//在service层进行connection连接的关闭
			//BaseDao.closeResource(connection, null, null);
			//utils.close(session);
			//int sum=5/0;
		}
		return flag;
	}
	@Override
	public User login(String userCode, String userPassword) {
		// TODO Auto-generated method stub
		Connection connection = null;
		User user = null;
		SqlSession session=null;
		try {
//			connection = BaseDao.getConnection();
//			user = userDao.getLoginUser(connection, userCode);
//			sqlSessionFactory=utils.open();
//			session=sqlSessionFactory.openSession(true);
//			user=session.getMapper(UserDao.class).getLoginUser(userCode);
			user=userDao.getLoginUser(userCode);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			//BaseDao.closeResource(connection, null, null);
			//utils.close(session);
		}
		//匹配密码
		if(null != user){
			if(!user.getUserPassword().equals(userPassword)) {
				user = null;
			}
		}
		return user;
	}
	@Override
	public List<User> getUserList(String queryUserName, int queryUserRole, int currentPageNo, int pageSize) {
		// TODO Auto-generated method stub
		Connection connection = null;
		List<User> userList = null;
		SqlSession session=null;
		System.out.println("queryUserName ---- > " + queryUserName);
		System.out.println("queryUserRole ---- > " + queryUserRole);
		System.out.println("currentPageNo ---- > " + currentPageNo);
		System.out.println("pageSize ---- > " + pageSize);
		try {
//			connection = BaseDao.getConnection();
//			userList = userDao.getUserList(connection, queryUserName,queryUserRole,currentPageNo,pageSize);
//			sqlSessionFactory=utils.open();
//			session=sqlSessionFactory.openSession(true);
			currentPageNo = (currentPageNo-1)*pageSize;
//			userList=session.getMapper(UserDao.class).getUserList(queryUserName,queryUserRole,currentPageNo,pageSize);
			userList=userDao.getUserList(queryUserName, queryUserRole, currentPageNo, pageSize);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			//BaseDao.closeResource(connection, null, null);
			//utils.close(session);
		}
		return userList;
	}
	@Override
	public User selectUserCodeExist(String userCode) {
		// TODO Auto-generated method stub
		Connection connection = null;
		User user = null;
		SqlSession session=null;
		try {
//			connection = BaseDao.getConnection();
//			user = userDao.getLoginUser(connection, userCode);
//			sqlSessionFactory=utils.open();
//			session=sqlSessionFactory.openSession(true);
//			user=session.getMapper(UserDao.class).getLoginUser(userCode);
			user=userDao.getLoginUser(userCode);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			//BaseDao.closeResource(connection, null, null);
			//utils.close(session);
		}
		return user;
	}
	@Override
	public boolean deleteUserById(Integer delId) {
		// TODO Auto-generated method stub
		Connection connection = null;
		SqlSession session=null;
		boolean flag = false;
		try {
//			connection = BaseDao.getConnection();
//			sqlSessionFactory=utils.open();
//			session=sqlSessionFactory.openSession(true);
			//session.getMapper(UserDao.class).deleteUserById(delId)
			if(userDao.deleteUserById(delId)> 0) {
				flag = true;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			//BaseDao.closeResource(connection, null, null);
			//utils.close(session);
		}
		return flag;
	}
	@Override
	public User getUserById(String id) {
		// TODO Auto-generated method stub
		User user = null;
		Connection connection = null;
		SqlSession session=null;
		try{
//			connection = BaseDao.getConnection();
//			user = userDao.getUserById(connection,id);
//			sqlSessionFactory=utils.open();
//			session=sqlSessionFactory.openSession(true);
//			user=session.getMapper(UserDao.class).getUserById(id);
			user=userDao.getUserById(id);
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			user = null;
		}finally{
			//BaseDao.closeResource(connection, null, null);
			//utils.close(session);
		}
		return user;
	}
	@Override
	public boolean modify(User user) {
		// TODO Auto-generated method stub
		Connection connection = null;
		SqlSession session=null;
		boolean flag = false;
		try {
//			connection = BaseDao.getConnection();
//			sqlSessionFactory=utils.open();
//			session=sqlSessionFactory.openSession(true);
			//session.getMapper(UserDao.class).modify(user)
			if( userDao.modify(user)> 0) {
				flag = true;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			//BaseDao.closeResource(connection, null, null);
			//utils.close(session);
		}
		return flag;
	}
	@Override
	public boolean updatePwd(int id, String pwd) {
		// TODO Auto-generated method stub
		boolean flag = false;
		Connection connection = null;
		SqlSession session=null;
		try{
//			connection = BaseDao.getConnection();
//			sqlSessionFactory=utils.open();
//			session=sqlSessionFactory.openSession(true);
			//session.getMapper(UserDao.class).updatePwd(id,pwd)
			if( userDao.updatePwd(id,pwd)> 0) {
				flag = true;
			}
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			//BaseDao.closeResource(connection, null, null);
			//utils.close(session);
		}
		return flag;
	}
	@Override
	public int getUserCount(String queryUserName, int queryUserRole) {
		// TODO Auto-generated method stub
		Connection connection = null;
		SqlSession session=null;
		int count = 0;
		System.out.println("queryUserName ---- > " + queryUserName);
		System.out.println("queryUserRole ---- > " + queryUserRole);
		try {
//			connection = BaseDao.getConnection();
//			count = userDao.getUserCount(connection, queryUserName,queryUserRole);
//			sqlSessionFactory=utils.open();
//			session=sqlSessionFactory.openSession(true);
//			count=session.getMapper(UserDao.class).getUserCount(queryUserName,queryUserRole);
			count=userDao.getUserCount(queryUserName, queryUserRole);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			//BaseDao.closeResource(connection, null, null);
			//utils.close(session);
		}
		return count;
	}
	
}
